package com.study.jyg.rxjava2study.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.utils.ActivityManager;
import com.study.jyg.rxjava2study.utils.status.StatusUtils;

import butterknife.ButterKnife;

public abstract class BasicActivity extends AppCompatActivity {


    protected String TAG;
    protected Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getTitleSetting());
        setContentView(getLayout());
        mContext = this;
        ActivityManager.getAppManager().addActivity(this);
        TAG = getLocalClassName();
        Log.e("Activity", TAG);
        ButterKnife.bind(this);
        init();
        initView();
    }

    private void setTitle(boolean b){
        if (!b)
            return;
        StatusUtils.setStatusBar(this, isUseThemestatusBarColor(), isWithoutUseStatusBarColor(), getStatusBarColor());
    }
    protected boolean getTitleSetting(){
        return true;
    }
    protected int getStatusBarColor(){
        return R.color.colorPrimary;
    }

    protected boolean isUseThemestatusBarColor() {
        return true;
    }

    protected boolean isWithoutUseStatusBarColor() {
        return true;
    }

    protected abstract int getLayout();
    protected void init(){}
    protected abstract void initView();

    /**
     * 是否设置menu
     *
     * @return
     */
    protected boolean isSetMenu() {
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        if (isSetMenu())
            getMenuInflater().inflate(getMenulayout(), menu);
        return true;
    }

    protected int getMenulayout() {
        return R.menu.menu_main;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            Toast.makeText(this, "显示menu", Toast.LENGTH_SHORT).show();
//            return true;
//        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 跳转
     * @param acitivity 活动
     */
    protected void startActivity( Class<?> acitivity){
        startActivity(acitivity, null);
    }

    protected void startActivity( Class<?> acitivity, Bundle bundle){
        Intent intent = new Intent(mContext, acitivity);
        intent.putExtras(null == bundle ? new Bundle() : bundle);
        startActivity(intent);
    }

    public void onBackClick(){
        finish();
    }

}
